<?php
/**
 * Created by PhpStorm.
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class UserFollow extends Model
{
    //set talbename
    protected $table = 'user_follow';
    //set primary key
    protected $pk = 'id';

    /**
     * 关注
     *
     */
    public function follow($arrInput)
    {
        unset($arrInput[$this->pk]);

        $this->data($arrInput);
        $this->save();
        return $this->data[$this->pk];
    }

    /**
     * 取消关注
     *
     */
    public function cancelFollow($arrInput)
    {

        if (empty($arrInput['userid']) || empty($arrInput['follow_type']) || empty($arrInput['follow_id'])) {
            return false;
        }

        $conds = [
            'userid' => ['=', $arrInput['userid']],
            'follow_type' => ['=', $arrInput['follow_type']],
            'follow_id' => ['=', $arrInput['follow_id']],
        ];

        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * 是否关注
     *
     */
    public function isFollow($userid, $follow_type, $follow_id)
    {
        $this->allowField(true);

        $arrCond = [
            'userid' => ['=', $userid],
            'follow_type' => ['=', $follow_type],
            'follow_id' => ['=', $follow_id],
        ];

        $count = Db::table($this->table)->where($arrCond)->count();
        if (false === $count) {
            return false;
        }
        return $count;
    }

    /**
     * 获取关注列表
     *
     */
    public function getFollow($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * 获取用户关注数
     *
     */
    public function getUserFollowCountByType($userid, $follow_type)
    {
        $this->allowField(true);

        $arrCond = [
            'userid' => ['=', $userid],
            'follow_type' => ['=', $follow_type],
        ];

        return Db::table($this->table)->where($arrCond)->count();
    }

    /**
     * 获取某类型被关注数
     *
     */
    public function getTypeFollowCount($follow_type)
    {
        $this->allowField(true);

        $arrCond = [
            'follow_type' => ['=', $follow_type],
        ];

        return Db::table($this->table)->where($arrCond)->count();
    }

    /**
     * 获取某类型某id被关注数
     *
     */
    public function getFollowidFollowCount($follow_id)
    {
        $this->allowField(true);

        $arrCond = [
            'follow_id' => ['=', $follow_id],
        ];

        return Db::table($this->table)->where($arrCond)->count();
    }

}
